package com.entfrm.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.entfrm.base.api.R;
import com.entfrm.log.annotation.SysLog;
import com.entfrm.security.entity.EntfrmUser;
import com.entfrm.security.util.SecurityUtil;
import com.entfrm.system.entity.*;
import com.entfrm.system.service.*;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@AllArgsConstructor
@RequestMapping("/deptDocument")
public class DpetDocumentController {
    private final KDeptUserService deptUserService;

    private final DeptService deptService;

    private final UserCardsService userCardsService;

  

    private QueryWrapper<DeptUser> getQueryWrapper(DeptUser deptUser) {
        return new QueryWrapper<DeptUser>().like(StrUtil.isNotBlank(deptUser.getName()), "name", deptUser.getName()).like(StrUtil.isNotBlank(deptUser.getPostion()),"postion",deptUser.getPostion()).eq(StrUtil.isNotBlank(deptUser.getDeptCode()), "dept_code", deptUser.getDeptCode());
    }
    private QueryWrapper<UserCards> getQueryWrapper(UserCards userCards) {
        return new QueryWrapper<UserCards>().eq(StrUtil.isNotBlank(userCards.getUserId()+""), "user_id", userCards.getUserId());
    }

   @GetMapping("/list")
   @ResponseBody
   public R list(Page page, DeptUser deptUser) {
       EntfrmUser user = SecurityUtil.getUser();
       deptUser.setDeptCode(user.getDeptId()+"");
     IPage<DeptUser> deptUserPage = deptUserService.page(page, getQueryWrapper(deptUser));
      return R.ok(deptUserPage.getRecords(), deptUserPage.getTotal());
    }

    @SysLog("部门人员新增")
    @SaCheckPermission("deptUser_add")
    @PostMapping("/save")
    public R save(@RequestBody DeptUser deptUser) {
        EntfrmUser user = SecurityUtil.getUser();
        deptUser.setDeptCode(user.getDeptId()+"");
        deptUser.setDelFlag("0");
        Dept dept=deptService.getById(user.getDeptId());
        deptUser.setDeptName(dept.getName());
        deptUserService.save(deptUser);
        return R.ok();
    }

    @SysLog("部门人员修改")
    @PutMapping("/update")
    @SaCheckPermission("deptUser_edit")
    @ResponseBody
    public R update(@RequestBody DeptUser deptUser) {
        deptUserService.updateById(deptUser);
        return R.ok();
    }
    @SysLog("证件完善")
    @PostMapping("/insertZJ")
    @SaCheckPermission("perfect_info")
    @ResponseBody
    public R insertZJ(@RequestBody List<UserCards> userCardsList) {
        UserCards userCards=new UserCards();
        userCards.setUserId(userCardsList.get(0).getUserId());
        for(UserCards userCards1 :userCardsList){
            userCards1.setDelFlag("0");
            userCards1.setId(null);
        }
        List<UserCards>  list= userCardsService.list(getQueryWrapper(userCards));
        if(list.size()>0 && list !=null){
           userCardsService.remove(getQueryWrapper(userCards));
        }
        userCardsService.saveBatch(userCardsList);
        return R.ok();
    }
    @GetMapping("/userCardList")
    @ResponseBody
    public R list(Page page, UserCards userCards) {
        IPage<DeptUser> deptUserPage = userCardsService.page(page, getQueryWrapper(userCards));
        return R.ok(deptUserPage.getRecords(), deptUserPage.getTotal());
    }
    @SysLog("部门人员信息删除")
    @SaCheckPermission("deptUser_del")
    @DeleteMapping("/remove/{id}")
    public R remove(@PathVariable("id") Integer[] id) {
        return R.ok(deptUserService.removeByIds(Arrays.asList(id)));
    }

}
